Java JavaScript Python C# C C++ Go Kotlin PHP Swift R Ruby TypeScript Scala SQL Perl rust VisualBasic Matlab Julia

While Loop

While in Java

What is While Loop?

The while loop is a control flow statement that allows you to execute a block of code repeatedly until a certain condition is met. The while loop has two parts: Condition: This is a boolean expression that is evaluated before each iteration of the loop. If the condition is true, then the block of code is executed. Body: This is the block of code that is executed if the condition is true. Here is the syntax for the while loop:
While syntax
while (condition) { // block of code }
The condition is evaluated before each iteration of the loop. If the condition is true, then the block of code is executed. The loop will continue to iterate until the condition is false. Here is an example of a while loop:
While - basic example
class Main{ public static void main(String[] args) { int i = 0; while (i < 10) { System.out.println(i); i++; } } }
In this example, the while loop will print the numbers from 0 to 9.

Output

1 2 3 4 5 6 7 8 9 10
The condition in this example is i < 10;. This statement checks if the value of i is less than 10. If it is, then the block of code is executed. The body in this example is the statement System.out.println(i);. This statement prints the value of i to the console. The while loop is a versatile control flow statement that can be used to implement a variety of looping patterns. It is a good choice when you do not know how many times you want to loop. Here are some additional things to keep in mind about while loops: The condition can be any boolean expression. The body can be any block of statements. The while loop can be nested. However, it is important to note that the while loop can cause an infinite loop if the condition is never set to false. It is important to ensure that the condition will eventually be set to false so that the loop will terminate.

  📌TAGS

★while ★while loop ★while syntax ★syntax ★ example

Tutorials